<?php


namespace app\common;

class Mail
{
	public static function get_verification_code($config, $account, $title, $data = [])
	{
		$today = \think\helper\Time::today();
		if (\think\Db::name("verification_code_log")->where([["time", "between", $today], ["mode", "=", model\VerificationLog::Mode_Sms], ["", "exp", \think\Db::raw("ip ='" . \think\facade\Request::ip() . "' or account='{$account}'")]])->count() >= 30) {
			return ["code" => 10235, "msg" => "验证码获取次数上限"];
		}
		$last_time = \think\Db::name("verification_code_log")->where([["", "exp", \think\Db::raw("ip ='" . \think\facade\Request::ip() . "' or account='{$account}'")], ["mode", "=", model\VerificationLog::Mode_Sms]])->order("id", "desc")->find();
		if (!empty($last_time) && \time() - $last_time["time"] < 59) {
			return ["code" => 10236, "msg" => "验证码获取频繁，请稍等一会重试"];
		}
		$code = rand(1000, 9999);
		$result = mail\PHPMailer::send($config, $title, "您的验证码为: {$code} ,请勿泄露！", $account);
		if ($result === true) {
			$expire = time() + 600;
			\think\Db::name("verification_code_log")->insert(array_merge(["mode" => model\VerificationLog::Mode_Mail, "account" => $account, "code" => $code, "time" => time(), "expire" => $expire, "ip" => \think\facade\Request::ip()], $data));
			return ["code" => 10000, "msg" => "验证码已发送成功"];
		} else {
			return ["code" => 10237, "msg" => "验证码发送失败，" . $result];
		}
	}
}